import Syunew6W from './Syunew6W'
import store from '@/store'

class NewSyunew6W extends Syunew6W{
  constructor() {
		super()
    this.Socket_UK.onerror = (event) => {
      // alert("未能连接服务程序，请确定服务程序是否安装。");
      store.dispatch('app/toggleUkeyStatus', true);
    };
  }
  err_Connect = function () {
    if (NewSyunew6W.bConnect == 1) return;
    // alert("未能连接服务程序，请确定服务程序是否安装。");
    store.dispatch('app/toggleUkeyStatus', true);
  };
  SendCmdAndWait = function (
    IsReturnErr,
    fun,
    param1,
    param2,
    param3,
    param4,
    param5,
    param6,
    param7,
    param8,
    param9,
    param10
  ) {
    var UK;

    return new Promise((resolve, reject) => {
      if (typeof MozWebSocket != "undefined") {
        UK = new MozWebSocket(this.url, "usbkey-protocol");
      } else {
        UK = new WebSocket(this.url, "usbkey-protocol");
      }
      try {
        UK.onopen = function () {
          NewSyunew6W.ResetOrder(UK); //
        };

        UK.onmessage = function got_packet(Msg) {
          var UK_Data = JSON.parse(Msg.data);
          var return_value;
          if (UK_Data.type != "Process") return; //
          switch (UK_Data.order) {
            case 0:
              {
                fun(
                  UK,
                  param1,
                  param2,
                  param3,
                  param4,
                  param5,
                  param6,
                  param7,
                  param8,
                  param9,
                  param10
                );
              }
              break; //!!!!!
            case 1:
              {
                NewSyunew6W.LastError = UK_Data.LastError;
                return_value = UK_Data.return_value;
                if (NewSyunew6W.LastError != 0) {
                  if (IsReturnErr) return_value = NewSyunew6W.LastError;
                }
                //
                UK.close();

                resolve(return_value);
              }
              break;
          }
        };
        UK.onclose = function () {};
        UK.onerror = (event) => {
          // alert("未能连接服务程序，请确定服务程序是否安装。");
          store.dispatch('app/toggleUkeyStatus', true);
        };
      } catch (e) {
        alert(e.name + ": " + e.message);
        resolve(false);
      }
    });
  };
  YWrite = function (indata, address, nlen, HKey, LKey, KeyPath) {
    var UK;

    return new Promise((resolve, reject) => {
      if (typeof MozWebSocket != "undefined") {
        UK = new MozWebSocket(this.url, "usbkey-protocol");
      } else {
        UK = new WebSocket(this.url, "usbkey-protocol");
      }

      try {
        var i = 0,
          order;
        UK.onopen = function () {
          NewSyunew6W.ResetOrder(UK); //
        };

        UK.onmessage = function got_packet(Msg) {
          var UK_Data = JSON.parse(Msg.data);
          var return_value;
          if (UK_Data.type != "Process") return; //
          if (UK_Data.order < nlen) {
            NewSyunew6W._SetBuf(UK, indata[UK_Data.order], UK_Data.order);
            return;
          } else {
            order = UK_Data.order - nlen;
          }

          switch (order) {
            case 0:
              {
                NewSyunew6W.LastError = UK_Data.LastError;
                if (NewSyunew6W.LastError != 0) {
                  UK.close();
                  resolve(UK_Data.return_value);
                  return;
                }
                NewSyunew6W._YWrite(UK, address, nlen, HKey, LKey, KeyPath);
              }
              break; //!!!!!
            case 1:
              {
                NewSyunew6W.LastError = UK_Data.LastError;
                return_value = UK_Data.return_value;
                if (NewSyunew6W.LastError != 0) {
                  return_value = NewSyunew6W.LastError;
                }

                UK.close();

                resolve(return_value);
              }
              break;
          }
        };
        UK.onclose = function () {};
        UK.onerror = (event) => {
          // alert("未能连接服务程序，请确定服务程序是否安装。");
          store.dispatch('app/toggleUkeyStatus', true);
        };
      } catch (e) {
        alert(e.name + ": " + e.message);
        resolve(false);
      }
    });
  };
  YRead = function (address, nlen, HKey, LKey, KeyPath) {
    var outb = new Uint8Array(nlen);
    var UK;
    return new Promise((resolve, reject) => {
      if (typeof MozWebSocket != "undefined") {
        UK = new MozWebSocket(this.url, "usbkey-protocol");
      } else {
        UK = new WebSocket(this.url, "usbkey-protocol");
      }

      try {
        var i, order;
        UK.onopen = function () {
          NewSyunew6W.ResetOrder(UK); //
        };

        UK.onmessage = function got_packet(Msg) {
          var UK_Data = JSON.parse(Msg.data);
          var return_value;
          if (UK_Data.type != "Process") return; //
          if (UK_Data.order < 2) {
            switch (UK_Data.order) {
              case 0:
                {
                  NewSyunew6W._YRead(UK, address, nlen, HKey, LKey, KeyPath);
                }
                break; //!!!!!
              case 1:
                {
                  NewSyunew6W.LastError = UK_Data.LastError;
                  if (NewSyunew6W.LastError != 0) {
                    UK.close();
                    resolve(outb);
                    return;
                  }

                  i = 0;
                  NewSyunew6W._GetBuf(UK, i); //
                }
                break;
            }
          } else {
            NewSyunew6W.LastError = UK_Data.LastError;
            if (NewSyunew6W.LastError != 0) {
              UK.close();
              resolve(outb);
              return;
            }
            outb[i] = UK_Data.return_value;
            i++;
            if (UK_Data.LastError != 0 || i >= nlen) {
              UK.close();
              resolve(outb);
              return;
            }
            NewSyunew6W._GetBuf(UK, i); //
          }
        };
        UK.onclose = function () {};
        UK.onerror = (event) => {
          // alert("未能连接服务程序，请确定服务程序是否安装。");
          store.dispatch('app/toggleUkeyStatus', true);
        };
      } catch (e) {
        alert(e.name + ": " + e.message);
        resolve(false);
      }
    });
    return outb;
  };
  StarGenKeyPair = function (KeyPath) {
    var KeyPairInfo = {
      GenPriKey: "",
      GenPubKeyX: "",
      GenPubKeyY: "",
    };
    var UK;

    return new Promise((resolve, reject) => {
      if (typeof MozWebSocket != "undefined") {
        UK = new MozWebSocket(this.url, "usbkey-protocol");
      } else {
        UK = new WebSocket(this.url, "usbkey-protocol");
      }

      try {
        UK.onopen = function () {
          NewSyunew6W.ResetOrder(UK); //
        };

        UK.onmessage = function got_packet(Msg) {
          var UK_Data = JSON.parse(Msg.data);
          var return_value;
          if (UK_Data.type != "Process") return; //
          switch (UK_Data.order) {
            case 0:
              {
                NewSyunew6W._StarGenKeyPair(UK, KeyPath);
              }
              break; //
            case 1:
              {
                NewSyunew6W.LastError = UK_Data.LastError;
                if (NewSyunew6W.LastError != 0) {
                  UK.close();
                  resolve(KeyPairInfo);
                  return;
                }
                NewSyunew6W._GenPriKey(UK);
              }
              break;
            case 2:
              {
                NewSyunew6W.LastError = UK_Data.LastError;
                if (NewSyunew6W.LastError != 0) {
                  UK.close();
                  resolve(KeyPairInfo);
                  return;
                }
                KeyPairInfo.GenPriKey = UK_Data.return_value;
                NewSyunew6W._GenPubKeyX(UK);
              }
              break;
            case 3:
              {
                NewSyunew6W.LastError = UK_Data.LastError;
                if (NewSyunew6W.LastError != 0) {
                  UK.close();
                  resolve(KeyPairInfo);
                  return;
                }
                KeyPairInfo.GenPubKeyX = UK_Data.return_value;
                NewSyunew6W._GenPubKeyY(UK);
              }
              break;
            case 4:
              {
                NewSyunew6W.LastError = UK_Data.LastError;
                if (NewSyunew6W.LastError != 0) {
                  UK.close();
                  resolve(KeyPairInfo);
                  return;
                }
                KeyPairInfo.GenPubKeyY = UK_Data.return_value;

                UK.close();
                resolve(KeyPairInfo);
                return;
              }
              break;
          }
        };
        UK.onclose = function () {};
        UK.onerror = (event) => {
          // alert("未能连接服务程序，请确定服务程序是否安装。");
          store.dispatch('app/toggleUkeyStatus', true);
        };
      } catch (e) {
        alert(e.name + ": " + e.message);
        resolve(false);
      }
    });
  };
  SubCal = function (Fun, Inbuf, KeyPath) {
    var UK;
    var outb = new Uint8Array(8);
    return new Promise((resolve, reject) => {
      if (typeof MozWebSocket != "undefined") {
        UK = new MozWebSocket(this.url, "usbkey-protocol");
      } else {
        UK = new WebSocket(this.url, "usbkey-protocol");
      }

      try {
        var i = 0,
          order;
        var bIsEnc = true;
        UK.onopen = function () {
          NewSyunew6W.ResetOrder(UK); //
        };

        UK.onmessage = function got_packet(Msg) {
          var UK_Data = JSON.parse(Msg.data);
          var return_value;
          if (UK_Data.type != "Process") return; //
          if (bIsEnc) {
            if (UK_Data.order < 8) {
              NewSyunew6W._SetEncBuf(UK, Inbuf[UK_Data.order], UK_Data.order);
              return;
            } else {
              order = UK_Data.order - 8;
            }

            switch (order) {
              case 0:
                {
                  NewSyunew6W.LastError = UK_Data.LastError;
                  if (NewSyunew6W.LastError != 0) {
                    UK.close();
                    resolve(outb);
                    return;
                  }
                  Fun(UK, KeyPath);
                }
                break; //
              case 1:
                {
                  NewSyunew6W.LastError = UK_Data.LastError;
                  return_value = UK_Data.return_value;
                  if (NewSyunew6W.LastError != 0) {
                    UK.close();
                    resolve(outb);
                    return;
                  }
                  bIsEnc = false;
                  i = 0;
                  NewSyunew6W.ResetOrder(UK);
                }
                break;
            }
          } else {
            NewSyunew6W.LastError = UK_Data.LastError;
            if (NewSyunew6W.LastError != 0) {
              UK.close();
              resolve(outb);
              return;
            }
            if (UK_Data.order < 8) {
              if (UK_Data.order > 0) {
                outb[i - 1] = UK_Data.return_value;
              }
              NewSyunew6W._GetEncBuf(UK, i); //
              i++;
            } else {
              outb[i - 1] = UK_Data.return_value;
              UK.close();
              resolve(outb);
              return;
            }
          }
        };
        UK.onclose = function () {};
        UK.onerror = (event) => {
          // alert("未能连接服务程序，请确定服务程序是否安装。");
          store.dispatch('app/toggleUkeyStatus', true);
        };
      } catch (e) {
        alert(e.name + ": " + e.message);
        resolve(false);
      }
    });
  };
}
// NewSyunew6W.prototype.constructor.bind(Syunew6W, this)
export default NewSyunew6W